package code12线程;

/**
 * @author 黄衡熙
 * @version 1.0
 */
/*
 * 既有两个房间A和B，此时的A中住着C手中拿着B房间的钥匙，B中住着D拿着A房间的钥匙，
 * 此时C和D都被锁在房间内，只有拿到钥匙才能打开门，因此进入死循环
 * */
public class Test06_死锁问题 {
    public static void main(String[] args) {
        Object obj1 = new Object();
        Object obj2 = new Object();
        ThreadA a = new ThreadA(obj1, obj2);
        ThreadB b = new ThreadB(obj1, obj2);
        Thread t1 = new Thread(a);
        Thread t2 = new Thread(b);
        t1.start();
        t2.start();
    }

}

class ThreadA implements Runnable {
    private Object obj1;
    private Object obj2;

    public ThreadA(Object obj1, Object obj2) {
        this.obj1 = obj1;
        this.obj2 = obj2;
    }

    @Override
    public void run() {
        synchronized (obj1) {
            System.out.println("ThreadA get obj1");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (obj2) {
                System.out.println("ThreadA get obj2");
            }
        }
    }
}

class ThreadB implements Runnable {
    private Object obj1;
    private Object obj2;

    public ThreadB(Object obj1, Object obj2) {
        this.obj1 = obj1;
        this.obj2 = obj2;
    }

    @Override
    public void run() {
        synchronized (obj2) {
            System.out.println("ThreadB get obj2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            synchronized (obj1) {
                System.out.println("ThreadB get obj1");
            }
        }
    }
}
